package cn.mutsuki.micrometer.core;

import io.micrometer.core.instrument.Tag;

import java.util.*;
@Deprecated
public class MeterProperties {
    private Map<String, String[]> identities = new HashMap<>();
    private final MeterType meterType;

    public MeterProperties(MeterType meterType){
        this.meterType = meterType;
    }

    public MeterProperties(Map<String, String[]> identities, MeterType meterType){
        this(meterType);
        identitiesValidation(identities);
        this.identities = identities;
    }

    public MeterType getMeterType(){
        return this.meterType;
    }

    public void setIdentities(Map<String,String[]> identities){
        identitiesValidation(identities);
        this.identities = identities;
    }

    public void addIdentity(MeterIdentity meterIdentity){
        identities.put(meterIdentity.getMeterName() , meterIdentity.getArrayTags());
    }

    private void identitiesValidation(Map<String,String[]> identities){
        identities.forEach((name,tags) -> {
            if(tags.length %2 == 1) throw new IllegalArgumentException("Tags length is not correctly, length: " + tags.length);
        });
    }

    public Map<String, String[]> getIdentities(){
        return identities;
    }

    public static class MeterIdentity{
        private String meterName;
        private List<String> listTags;
        private Map<String , String> mapTags;

        private Map<String , String> tagsToMap(String... tags){
            Map<String , String> mapTags = new HashMap<>();
            if(tags.length %2 == 1) throw new IllegalArgumentException("Tags length is not correctly, length: " + tags.length);
            for(int i = 0 ; i < tags.length ; i+=2){
                mapTags.put(tags[i] , tags[i+1]);
            }
            return mapTags;
        }

        public MeterIdentity(String meterName , String... tags){
            this.meterName = meterName;
            this.listTags = Arrays.asList(tags);
            this.mapTags = tagsToMap(tags);
        }

        public String getMeterName(){
            return this.meterName;
        }

        public void setMeterName(String meterName){
            this.meterName = meterName;
        }

        public String[] getArrayTags(){
            String[] str = new String[listTags.size()];
            return listTags.toArray(str);
        }

        public void addTag(String tagName , String value){
            listTags.add(tagName);
            listTags.add(value);
            mapTags.put(tagName , value);
        }

        public void addTag(Tag tag){
            listTags.add(tag.getKey());
            listTags.add(tag.getValue());
            mapTags.put(tag.getKey() , tag.getValue());
        }

        public String getTagValue(String tag){
            return mapTags.get(tag);
        }
    }

    public enum MeterType{
        COUNTER,GAUGE
    }
}
